<?php

// No direct access to this file
defined('_JEXEC') or die('Restricted access');
JTable::addIncludePath(JPATH_COMPONENT.DIRECTORY_SEPARATOR.'tables');

class KetnoiModelInventory extends JModelLegacy
{
	public function getTable($type = 'Inventory', $prefix = 'KetnoiTable', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}
	
	function loadGridData($limitstart,$limit,$where)
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("i.*,p.ma as ma_machine,p.ten as ten_machine");
        $query->select("v.ma as ma_nhacungcap,v.ten as tennhacungcap");
        $query->from("#__inventory i")
       		  ->innerJoin("#__products p on p.id = i.machine_id")	
       		  ->leftJoin("#__vendor v on v.id = i.vendor_id");
        $query->where($where);
        $query .= " limit $limitstart,$limit";  
        $db->setQuery($query);
        $result = $db->loadObjectList();
        return $result; 
    }
    
	function loadAllGridData($where)
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("count(*)");
        $query->select("v.ma as ma_nhacungcap,v.ten as tennhacungcap");
        $query->from("#__inventory i")
       		  ->innerJoin("#__products p on p.id = i.machine_id")	
       		  ->leftJoin("#__vendor v on v.id = i.vendor_id");
        $query->where($where); 
        $db->setQuery($query);
        $result = $db->loadResult();
        return $result; 
    }
    
	function save()
    {
    	global $app;
    	$result = array("success"=>true,"error"=>"");
    	$table = $this->getTable();
    	$post = JRequest::get("post");
		$table = Utility::save($table, $post);
		$result["id"] = $table->id;
    	return $result;
    }
    
    function loadData($id)
    {
    	$table = $this->getTable();
    	$table->load($id);
    	return $table;
    }
    
	function checkSerial()
    {
    	$serial = JRequest::getVar("serial");
    	$db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("count(*)");
        $query->from("#__inventory");
        $query->where(" serial = '$serial'");
        $db->setQuery($query);
        $result = $db->loadResult();
        return $result > 0;
    }
    /*
	function delete()
	{
		$id = JRequest::getVar("id");
		$db = JFactory::getDbo();
		$query = "update #__vendor set is_delete = 1 where id = $id";
		$db->setQuery($query);
		$db->query();

	}*/

	function loadAllVendor()
	{
		$db = JFactory::getDbo();
		$query = "select * from #__vendor where is_delete = 0";
		$db->setQuery($query);
		return $db->loadObjectList();
	}
	
	function getInventoryStatus($inventory)
	{
		$db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("hs.ten,hs.id");
        $query->from("#__hopdong h");
        $query->innerJoin("#__hopdong_status hs on hs.id = h.status");
        $query->where("(h.status = 'dachothue' or h.status = 'daban') and h.inventory_id = $inventory");
        $db->setQuery($query);
        return $db->loadObject();
	}
	
	function delete()
	{
		$id = JRequest::getVar("id");
		$db = JFactory::getDbo();
		$query = "update #__inventory set is_delete = 1 where id = $id";
		$db->setQuery($query);
		$db->query();
	}
	
	function loadGridDataLinhkien($where)
    {
    	$db = JFactory::getDbo();
    	$query = $db->getQuery(true);
    	$query->select("a.*,d.ten as tenlinhkien,d.ma as malinhkien,b.closing")
    		  ->select("case when e.ten is null then '' else e.ten end as tennhacungcap")
    		  ->select("case when e.ma is null then '' else e.ma end as manhacungcap")
    	      ->from("#__repair_linhkien a")
    	      ->innerJoin("#__repair_linhkien_closing b on b.repair_linhkien_id = a.id")
    	      ->innerJoin("#__repair c on b.repair_id = c.id")
    	      ->innerJoin("#__products d on d.id = a.linhkien_id and d.product_type='linhkien'")
    	      ->leftJoin("#__vendor e on e.id = a.vendor_id")
    	      ->where("a.status = 0 and b.status = 0");
    	$query->where($where);      
    	$db->setQuery($query);
    	return $db->loadObjectList();      
    }
}
